<?php
namespace app\backstage\controller;
use app\backstage\classes\helper\XlsTools;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Controller;
use think\Db;
use think\facade\Request;
class Withdrawal extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    /*
     * 代理提现明细列表
     * */
    public function index_agent()
    {
        $params = Request::param('', '', 'strip_tags');

        $this->assign('id',$params['id']);
        return $this->fetch('agent_list');
    }

    /*提现明细列表*/
    public function index()
    {
        $ManagerUser = \think\facade\App::model('Account', 'logic');
        $agents = $ManagerUser->getAgents();

        $this->assign('agents',$agents);

        if(request()->_role->alias == 'agent')
        {
            $withdrawal_amount = getByWithdrawalAmountAgent(request()->_user->id);
            $commission_amount = getByCommissionAmountAgent(request()->_user->id);
            $balance = $commission_amount - $withdrawal_amount;

            $this->assign('balance',getQuotient($balance,100));
            $this->assign('withdrawal_amount',getQuotient($withdrawal_amount,100));
            $this->assign('commission_amount',getQuotient($commission_amount,100));
            return $this->fetch('index_agent');
        }

        $withdrawal_amount = getByWithdrawalAmount();
        $commission_amount = getByCommissionAmount();
        $balance = $commission_amount - $withdrawal_amount;

        $this->assign('balance',getQuotient($balance,100));
        $this->assign('withdrawal_amount',getQuotient($withdrawal_amount,100));
        $this->assign('commission_amount',getQuotient($commission_amount,100));
        return $this->fetch();
    }

    /*提现明细列表数据获取*/
    public function list_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $Withdrawal = \think\facade\App::model('Withdrawal', 'logic');
        $result = $Withdrawal->getList($params);

        if ($result['count'] > 0) {
            return ['code' => 0, 'msg' => '数据获取成功', 'count' => $result['count'], 'data' => $result['list']];
        }

        return ['code' => 1, 'msg' => '暂无数据', 'count' => $result['count'], 'data' => $result['list']];
    }

    /*提现信息审核*/
    public function examine()
    {
        $Withdrawal = \think\facade\App::model('Withdrawal','logic');

        if( Request::isGet() ){

            $params = Request::param('', '', 'strip_tags');

            $where[] = ['id','=',$params['id']];

            if (request()->_role->alias == 'agent') {
                $where[] = ['agent_id','=',request()->_user->id];
            }

            $info = $Withdrawal->getInfo($where);

            if(!$info){
                $this->error('该信息记录不存在');
            }

            $this->assign('info',$info);
            return $this->fetch();
        }

        $params = Request::post('','','strip_tags,trim');
        $verify_result = $this->validate($params, 'Withdrawal.info');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $result = $Withdrawal->examineInfo($params);

        return json($result);
    }

    /*确认已打款*/
    public function confirm_payment()
    {
        $params = Request::param('','','strip_tags,trim');
        $verify_result = $this->validate($params, 'Withdrawal.info');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Withdrawal = \think\facade\App::model('Withdrawal','logic');
        $result = $Withdrawal->confirmPayment($params);

        return json($result);
    }

    /*申请提现*/
    public function add()
    {
        if( Request::isGet() ){

            $record = Db::table('cms_bank_card')->where('status',2)->where('agent_id',request()->_user->id)
                ->order('id DESC')->findOrEmpty();

            $withdrawal_amount = getByWithdrawalAmountAgent(request()->_user->id);
            $commission_amount = getByCommissionAmountAgent(request()->_user->id);
            $banance = $commission_amount - $withdrawal_amount;

            $date = date('Y-m-d');
            $count = Db::table('cms_withdrawal_record')->where('agent_id',request()->_user->id)->where('create_date',$date)->count();

            $this->assign('count',10-$count);
            $this->assign('balance',getQuotient($banance,100));
            $this->assign('record',$record);
            return $this->fetch();
        }
        if (request()->_user->allow_cache != 1) {
            return json(['code' => 101, 'msg' => '提现权限关闭']);
        }
        $params = Request::post('','','strip_tags');
        $verify_result = $this->validate($params, 'Withdrawal.add');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Withdrawal = \think\facade\App::model('Withdrawal', 'logic');
        $result = $Withdrawal->addInfo($params);

        return json($result);
    }

    /*
     * 按结算日期获取可提现余额
     * */
    public function agent_balance()
    {
        $params = Request::post('','','strip_tags');

        $balance = Db::table('rp_income_summary_daily_agent')
            ->where('report_date','<=',$params['settlement_date'])
            ->where('status','=',1)
            ->where('agent_id','=',request()->_user->id)->sum('commission_amount');

        return json(['code'=>100,'balance'=>getQuotient($balance,100)]);
    }

    /*
     * 代理提现汇总
     * */
    public function agent_summary()
    {
        $ManagerUser = \think\facade\App::model('Account', 'logic');
        $agents = $ManagerUser->getAgents();
        $ebs = $ManagerUser->getEbs();

        //总充值
        $recharge_amount = Db::table('rp_income_summary_daily_agent')->sum('recharge_amount');
        //总分成
        $commission_amount = Db::table('rp_income_summary_daily_agent')->sum('commission_amount');
        //提现中
        $wp_amount = Db::table('cms_withdrawal_record')->where('status',2)->sum('money');
        //已打款
        $paid_amount = Db::table('cms_withdrawal_record')->where('status',3)->sum('money');

        //未提现
        $ud_amount = $commission_amount - ($paid_amount+$wp_amount);

        $info['recharge_amount']    = getQuotient($recharge_amount,100);
        $info['commission_amount']  = getQuotient($commission_amount,100);
        $info['wp_amount']          = getQuotient($wp_amount,100);
        $info['paid_amount']        = getQuotient($paid_amount,100);
        $info['ud_amount']          = getQuotient($ud_amount,100);


        $this->assign('info',$info);
        $this->assign('agents',$agents);
        $this->assign('ebs',$ebs);
        return $this->fetch();
    }

    /*
     * 代理提现汇总列表数据
     * */
    public function agent_summary_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $Withdrawal = \think\facade\App::model('Withdrawal', 'logic');
        $result = $Withdrawal->getAgentSummaryList($params);

        if ($result['count'] > 0) {
            return ['code' => 0, 'msg' => '数据获取成功', 'count' => $result['count'], 'data' => $result['list']];
        }

        return ['code' => 1, 'msg' => '暂无数据', 'count' => $result['count'], 'data' => $result['list']];
    }

    /*
     * 代理提现汇总数据导出
     * */
    public function agent_summary_export()
    {
        $params = Request::param('', '', 'strip_tags');
        $params['page'] = 1;
        $params['limit'] = 1000000;
        $Withdrawal = \think\facade\App::model('Withdrawal', 'logic');
        $result = $Withdrawal->getAgentSummaryList($params);

        $this->assign('list',$result['list']);

        $date = date('YmdHis');
        header("Content-Type: application/vnd.ms-excel; name='excel'");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=xls-{$date}(代理提现汇总).xls");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/download");
        header("Pragma: no-cache");
        header("Expires: 0");

        echo $this->fetch()->getContent();
    }

}